package com.bird.thread.base.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author: bird
 * @Data: 2021/7/2 14:12
 * @description: 描述：演示 使用join和不使用join 线程的执行是顺序。
 */
public class Join {

    public static void main(String[] args) throws InterruptedException {



        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 子线程执行完毕");
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 子线程执行完毕");
            }
        });

        System.out.println("线程名称："+thread1.getName()+" thread1-new状态："+thread1.getState());

        thread1.start();
        System.out.println("线程名称："+thread1.getName()+ "thread1-runnable状态："+thread1.getState());
        thread2.start();
        System.out.println("开始等待子线程运行...");
        // 让子线程加入主线程 等子线程执行完毕后 才执行主线程。
        thread1.join();
        System.out.println("thread1-状态："+thread1.getState());
        thread2.join();

       System.out.println("所有子线程执行完毕");
    }


    /*
     * 总结：在主线程里面添加子线程,如果子线程使用join方法 那么主线程需要等待子线程执行完毕再执行。
     *
     *
     * */

}
